package com.ruoyi.info.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.health.domain.Favorite;
import com.ruoyi.health.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 收藏-->便民服务 Controller
 *
 * @author libin
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/info/healthFavorite")
public class FrontFavoriteController extends BaseController {

    @Autowired
    private IFavoriteService favoriteService;


    /**
     * 新增[收藏]   客户点收藏
     */
    @Log(title = "新增收藏便民服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Favorite favorite)
    {
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            favorite.setUserId(frontUserId);
            // TODO:不存在的便民服务id，不能收藏
            return toAjax(favoriteService.insertFavorite(favorite));
        }else{
            return AjaxResult.error("收藏失败！无法获取前台用户信息！");
        }

    }

    /**
     * 删除[收藏]
     */
    @Log(title = "取消收藏便民服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serviceId}")
    public AjaxResult remove(@PathVariable Integer serviceId){
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId) && StringUtils.isNotNull(serviceId)){
            //【frontUserId】用户取消对【resourceId】资源的收藏
            Favorite favorite = new Favorite();
            favorite.setUserId(frontUserId);
            favorite.setServiceId(serviceId);
            return toAjax(favoriteService.deleteFavorite(favorite));
        }else{
            return AjaxResult.error("取消收藏失败！无法获取前台用户信息！");
        }

    }


    /**
     * [收藏]列表
     */
    @GetMapping("/list")
    public TableDataInfo listByUserId() {
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            return getDataTable(favoriteService.selectFavoriteWithServiceByUserId(frontUserId));
        }else{
            return getDataTable(null);

        }

    }


    @GetMapping("/{serviceId}")
    public AjaxResult getFavoriteStatus(@PathVariable Integer serviceId) {
        //【注意，这里要获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        System.out.println(frontUserId);
        if(StringUtils.isNotNull(frontUserId)){
            Favorite favorite = new Favorite();
            favorite.setUserId(frontUserId);
            favorite.setServiceId(serviceId);
            //查询是否收藏了此 favorite
            int favoriteStatus = favoriteService.getFavoriteStatus(favorite);
            HashMap<String, Integer> map = new HashMap<>();
            map.put(Constants.STATUS,favoriteStatus);
            //返回结果
            return AjaxResult.success(map);
        }else{
            return AjaxResult.error(HttpStatus.GET_DATA_ERROR,"获取收藏信息失败！无法获取前台用户信息！");
        }

    }
}
